package com.zjdiepu.www.proxy.biz.impl.log;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.diepu.www.client.hzicbc.proxy.request.HzicbcBaseRequest;
import com.diepu.www.client.hzicbc.proxy.request.PublicRequest;
import com.diepu.www.client.hzicbc.proxy.response.HzicbcBaseResponse;
import com.diepu.www.client.proxy.hzicbc.request.CommonRequest;
import com.icbc.api.core.ApiResponse;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.consts.HzicbcService;
import com.zjdiepu.www.consts.ProxyService;
import com.zjdiepu.www.diepu.enums.ModeIDTypeDiepu;
import com.zjdiepu.www.enums.TransferModeID;
import com.zjdiepu.www.proxy.biz.transfer.ITransferYwOrdernoBankService;
import com.zjdiepu.www.proxy.dao.transfer.TransferLogsMapper;
import com.zjdiepu.www.proxy.dao.transfer.TransferLogsSubMapper;
import com.zjdiepu.www.proxy.dao.transfer.TransferYwOrdernoBankMapper;
import com.zjdiepu.www.proxy.model.dto.transfer.TransferLogs;
import com.zjdiepu.www.proxy.model.dto.transfer.TransferLogsSub;
import com.zjdiepu.www.proxy.model.dto.transfer.TransferYwOrdernoBank;
import com.zjdiepu.www.service.base.ICommonLogService;
/**
 * 记录日记
 * @author xiongxiaotun
 *
 */
@Service
public class CommonLogService implements ICommonLogService {

	@Autowired
	private TransferLogsMapper transferLogsMapper;
	
	@Autowired
	private TransferLogsSubMapper transferLogsSubMapper;

	@Autowired
	private TransferYwOrdernoBankMapper transferYwOrdernoBankMapper;
	
	@Autowired
	private ITransferYwOrdernoBankService transferYwOrdernoBankService;
	
	@Override
	@Transactional(isolation=Isolation.READ_COMMITTED,readOnly=false,propagation=Propagation.REQUIRES_NEW,timeout=60)
	public void insertCallbackLog(CommonRequest commonRequest,
			HzicbcBaseResponse hzicbcBaseResponse, String exceptionDes,
			String service, boolean statusEvent) {
		String reqparam = commonRequest.getReqparam();
		TransferModeID transferModeID = parseHzicbcModelName(service);
		TransferLogs transferLogs = new TransferLogs();
		transferLogs.initInsertData(SystemConsts.DEFAULT_ID, null != transferModeID ? transferModeID.getValue() : "未知中转模块");
		
		PublicRequest pub = null;
		if(StringUtils.isNotBlank(reqparam)) {
			HzicbcBaseRequest hzicbcBaseRequest = JSON.parseObject(reqparam, HzicbcBaseRequest.class);
			if(null != hzicbcBaseRequest) {
				pub = hzicbcBaseRequest.getPub();
			}
		}
		if(null != pub) {
			String orderno = pub.getOrderno();
			transferLogs.setBankOrderNo(orderno);
			transferLogs.setCmpSeq(pub.getCmpseq());
			String busitype_ = pub.getBusitype();
			String busitype = "";
			if("1".equals(busitype_)) {
				busitype = ModeIDTypeDiepu.HZICBC.getCode() + "_1"; //新车
			} else if("907".equals(busitype_)) {
				busitype = ModeIDTypeDiepu.HZICBC.getCode() + "_907"; //二手车
			} else {
				busitype = ModeIDTypeDiepu.HZICBC.getCode();
			}
			if(StringUtils.isNotBlank(orderno)) {
				transferLogs.setBusiType(busitype);
//				TransferYwOrdernoBank transferYwOrdernoBank = new TransferYwOrdernoBank();
//				transferYwOrdernoBank.setDpStatus(DpStatus.NORMAL.getCode());
//				transferYwOrdernoBank.setOrdeRno(orderno);
//				transferYwOrdernoBank.setOrderSql("`CREATED` DESC");
//				transferYwOrdernoBank = transferYwOrdernoBankMapper.selectRefined(transferYwOrdernoBank);

				TransferYwOrdernoBank transferYwOrdernoBank = transferYwOrdernoBankService.getByOrderNo(orderno);
				if(null != transferYwOrdernoBank) {
					transferLogs.setBusiId(transferYwOrdernoBank.getBusiId());
					transferLogs.setDataId(transferYwOrdernoBank.getBusiId());
				}
			}
		}
		transferLogsMapper.insert(transferLogs);
		
		TransferLogsSub transferLogsSub = new TransferLogsSub();
		transferLogsSub.initInsertData(SystemConsts.DEFAULT_ID, null != transferModeID ? transferModeID.getValue() : "未知中转模块");
		transferLogsSub.setTransferLogsId(transferLogs.getId());
		transferLogsSub.setDataId(transferLogs.getDataId());
		transferLogsSub.setBankOrderNo(transferLogs.getBankOrderNo());
		transferLogsSub.setCmpSeq(transferLogs.getCmpSeq());
		transferLogsSub.setBusiId(transferLogs.getBusiId());
		if(null != transferModeID) {
			transferLogsSub.setTransferModeId(transferModeID.getCode());
			transferLogsSub.setTransferModeName(transferModeID.getValue());
		}
		transferLogsSub.setRequestMessageEvent(JSON.toJSONString(commonRequest));
		String responseString = null;
		if(null != hzicbcBaseResponse) {
			responseString = JSON.toJSONString(hzicbcBaseResponse);
		}
		transferLogsSub.setResponseMessageEvent(responseString);
		transferLogsSub.setRemarks(exceptionDes);
		transferLogsSubMapper.insert(transferLogsSub);
		
	}
	
	@Override
	@Transactional(isolation=Isolation.READ_COMMITTED,readOnly=false,propagation=Propagation.REQUIRES_NEW,timeout=60)
	public void insertRequestLog(HzicbcBaseRequest hzicbcBaseRequest, ApiResponse apiResponse,
			String exceptionDes, String service, boolean statusEvent) {
		TransferModeID transferModeID = parseHzicbcModelName(service);
		TransferLogs transferLogs = new TransferLogs();
		transferLogs.initInsertData(SystemConsts.DEFAULT_ID, null != transferModeID ? transferModeID.getValue() : "未知中转模块");
		PublicRequest pub = hzicbcBaseRequest.getPub();
		if(null != pub) {
			String orderno = pub.getOrderno();
			transferLogs.setBankOrderNo(orderno);
			transferLogs.setCmpSeq(pub.getCmpseq());
			String busitype_ = pub.getBusitype();
			String busitype = "";
			if("1".equals(busitype_)) {
				busitype = ModeIDTypeDiepu.HZICBC.getCode() + "_1"; //新车
			} else if("907".equals(busitype_)) {
				busitype = ModeIDTypeDiepu.HZICBC.getCode() + "_907"; //二手车
			} else {
				busitype = ModeIDTypeDiepu.HZICBC.getCode();
			}
			if(StringUtils.isNotBlank(orderno)) {
				transferLogs.setBusiType(busitype);
//				TransferYwOrdernoBank transferYwOrdernoBank = new TransferYwOrdernoBank();
//				transferYwOrdernoBank.setDpStatus(DpStatus.NORMAL.getCode());
//				transferYwOrdernoBank.setOrdeRno(orderno);
//				transferYwOrdernoBank.setOrderSql("`CREATED` DESC");
//				transferYwOrdernoBank = transferYwOrdernoBankMapper.selectRefined(transferYwOrdernoBank);
				TransferYwOrdernoBank transferYwOrdernoBank = transferYwOrdernoBankService.getByOrderNo(orderno);
				if(null != transferYwOrdernoBank) {
					transferLogs.setBusiId(transferYwOrdernoBank.getBusiId());
					transferLogs.setDataId(transferYwOrdernoBank.getBusiId());
				}
			}
		}
		transferLogsMapper.insert(transferLogs);
		
		TransferLogsSub transferLogsSub = new TransferLogsSub();
		transferLogsSub.initInsertData(SystemConsts.DEFAULT_ID, null != transferModeID ? transferModeID.getValue() : "未知中转模块");
		transferLogsSub.setTransferLogsId(transferLogs.getId());
		transferLogsSub.setDataId(transferLogs.getDataId());
		transferLogsSub.setBankOrderNo(transferLogs.getBankOrderNo());
		transferLogsSub.setCmpSeq(transferLogs.getCmpSeq());
		transferLogsSub.setBusiId(transferLogs.getBusiId());
		if(null != transferModeID) {
			transferLogsSub.setTransferModeId(transferModeID.getCode());
			transferLogsSub.setTransferModeName(transferModeID.getValue());
		}
		transferLogsSub.setRequestMessageEvent(JSON.toJSONString(hzicbcBaseRequest));
		String responseString = null;
		if(null != apiResponse) {
			Map<Object, Object> response = new HashMap<Object, Object>();
			response.put("pub", apiResponse.getMap("pub"));
			response.put("ans", apiResponse.getMap("ans"));
			responseString = JSON.toJSONString(response);
		}
		transferLogsSub.setResponseMessageEvent(StringUtils.isBlank(responseString) ? exceptionDes : responseString);
		transferLogsSubMapper.insert(transferLogsSub);
	}
	
	private TransferModeID parseHzicbcModelName(String service) {
		if(HzicbcService.APPLY_DIVI_APINAME.equals(service)) {
			return TransferModeID.SQFQ;
		} else if(HzicbcService.APPLY_STATUS_APINAME.equals(service)) {
			return TransferModeID.CXFQZT;
		} else if(HzicbcService.FILE_DOWNLOAD_APINAME.equals(service)) {
			return TransferModeID.XZWJQD;
		} else if(HzicbcService.APPLY_DIVI_GENERAL_APINAME.equals(service)) {
			return TransferModeID.SQFQ;
		} else if(HzicbcService.CREDIT_CARD_APPLY.equals(service)) {
			return TransferModeID.SQKK;
		} else if(HzicbcService.APPLY_CREDIT.equals(service)) {
			return TransferModeID.SQCZX;
		} else if(HzicbcService.APPLY_CREDIT_STATUS_APINAME.equals(service)) {
			return TransferModeID.CHA_XUN_KAI_KA_JIN_DU;
		}
		
		else if(ProxyService.HAND_PIC.equals(service)) {
			return TransferModeID.SGHQFJ;
		} else if(ProxyService.BACK_NOTICE.equals(service)) {
			return TransferModeID.KKSQTH;
		} else if(ProxyService.APPLY_DIVI_BACK.equals(service)) {
			return TransferModeID.FQSQTH;
		} else if(ProxyService.FILE_LIST_NOTICE.equals(service)) {
			return TransferModeID.QDSCTZ;
		} else if(ProxyService.APPLY_CREDIT.equals(service)) {
			return TransferModeID.ZXJGHD;
		} else if(ProxyService.MULTIMEDIA_BACK.equals(service)) {
			return TransferModeID.MIAN_QIAN_SHI_PIN_TUI_HUI;
		}
		return null;
	}
	

}
